﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Threading;

namespace BrainTechLLC.WPF
{
    public class SimpleDispatcherTimer
    {
        public DispatcherTimer _timer;
        protected int _timerInterval;
        public int _doneNothingCount;
        public object Tag;
        public Func<SimpleDispatcherTimer, RunInitializationResult> RunInitialization;

        public SimpleDispatcherTimer(int timerMS, Func<SimpleDispatcherTimer, RunInitializationResult> runInitialization)
        {
            RunInitialization = runInitialization;
            _timerInterval = timerMS;
            _timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, _timerInterval) };
            _timer.Tick += new EventHandler(_timer_Tick);
        }

#if SILVERLIGHT
#else

        public SimpleDispatcherTimer(DispatcherPriority priority, int timerMS, Func<SimpleDispatcherTimer, RunInitializationResult> runInitialization)
        {
            RunInitialization = runInitialization;
            _timerInterval = timerMS;
            _timer = new DispatcherTimer(priority) { Interval = new TimeSpan(0, 0, 0, 0, _timerInterval) };
            _timer.Tick += new EventHandler(_timer_Tick);
        }
#endif

        public void Start()
        {
            _timer.Start();
        }

        public void Stop()
        {
            if (_timer != null)
            {
                _timer.Stop();
            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (RunInitialization != null)
            {
                _timer.Stop();
                var result = RunInitialization(this);

                if ((result & RunInitializationResult.DidntDoAnything) == RunInitializationResult.DidntDoAnything)
                {
                    _doneNothingCount++;
                }
                if ((result & RunInitializationResult.DidSomething) == RunInitializationResult.DidSomething)
                {
                    _doneNothingCount = 0;                    
                }

                if ((result & RunInitializationResult.StopTimer) == RunInitializationResult.StopTimer)
                {
                    Stop();
                }
                else if ((result & RunInitializationResult.DisposeTimer) == RunInitializationResult.DisposeTimer)
                {
                    DisposeTimer();
                }
                else
                {
                    _timer.Start();
                }
            }
        }

        public int TimerInterval
        {
            get { return _timerInterval; }
            set
            {
                if (_timerInterval != value)
                {
                    _timerInterval = value;
                    _timer.Interval = new TimeSpan(0, 0, 0, 0, _timerInterval);
                }
            }
        }

        public void DisposeTimer()
        {
            if (_timer != null)
            {
                Stop();
                _timer.Tick -= _timer_Tick;
                Tag = null;
                _timer = null;
            }

            RunInitialization = null;
        }
    }
}
